package com.shakespace.artofandroid

import android.app.Application
import android.util.Log
import com.shakespace.artofandroid.chapter13other.CrashHandler
import com.shakespace.artofandroid.global.Utils
import com.shakespace.firstlinecode.global.TAG

class App : Application() {


    override fun onCreate() {
        super.onCreate()
        app = this
        // 添加异常处理
        CrashHandler.getInstance().create(this)
        val processName = Utils.getProcessName(this)
        Log.e(this.TAG, "onCreate: -----------  $processName")
    }

    override fun onLowMemory() {
        super.onLowMemory()
        Log.e(this.TAG, "onLowMemory: ")
    }

    // 2020-01-07 NOTE:  TRIM_MEMORY_UI_HIDDEN mean ALL VIEWs of our app are invisible . not like onStop (jump from A Activity to B will call onStop , not onTrimMemory)
    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        var state = ""
        when (level) {
            TRIM_MEMORY_BACKGROUND -> state = "TRIM_MEMORY_BACKGROUND"
            TRIM_MEMORY_COMPLETE -> state = "TRIM_MEMORY_COMPLETE"
            TRIM_MEMORY_MODERATE -> state = "TRIM_MEMORY_MODERATE"
            TRIM_MEMORY_RUNNING_CRITICAL -> state = "TRIM_MEMORY_RUNNING_CRITICAL"
            TRIM_MEMORY_RUNNING_LOW -> state = "TRIM_MEMORY_RUNNING_LOW"
            TRIM_MEMORY_RUNNING_MODERATE -> state = "TRIM_MEMORY_RUNNING_MODERATE"
            // usually release some memory here
            TRIM_MEMORY_UI_HIDDEN -> state = "TRIM_MEMORY_UI_HIDDEN"
        }

        Log.e(this.TAG, "onTrimMemory: $state")
    }

    override fun onTerminate() {
        super.onTerminate()
        Log.e(this.TAG, "onTerminate: ")
    }

    companion object {
        var app: App? = null
        fun getInstance(): App? {
            return app
        }
    }
}